import { createRouter, createWebHistory } from "vue-router";

// 从目录动态导入路由
const pages = import.meta.glob("@/pages/*.vue", {
  eager: true,
  import: "default",
});
const routes = Object.entries(pages).map((item) => {
  const path =
    item[0] === "/src/pages/Home.vue"
      ? "/"
      : item[0].replace(".vue", "").replace("/src/pages", "");
  const name = item[1].name;
  const component = item[1];

  return {
    path,
    name,
    component,
  };
});

const router = createRouter({
  history: createWebHistory(),
  routes,
});

// 前置守卫 （浏览器标题名）
router.beforeEach((to, from, next) => {
  next();
  document.title = to.name;
});

export default router;
